<?php

namespace app\api\model;

use app\common\exception\BaseException;
use app\common\model\Activity as ActivityModel;

/**
 * 活动模型
 * Class Activity
 * @package app\api\model
 */
class Activity extends ActivityModel
{
    /**
     * 追加字段
     * @var array
     */
    protected $append = [
        'view_time'
    ];

    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'wxapp_id'
    ];

    /**
     * 活动详情：HTML实体转换回普通字符
     * @param $value
     * @return string
     */
    public function getActivityContentAttr($value)
    {
        return htmlspecialchars_decode($value);
    }

    public function getViewTimeAttr($value, $data)
    {
        return date('Y-m-d', $data['create_time']);
    }

    public function getStartSignAttr($value, $data)
    {
        return date('Y-m-d H:i', strtotime($data['start_sign']));
    }

    public function getEndSignAttr($value, $data)
    {
        return date('Y-m-d H:i', strtotime($data['end_sign']));
    }

    public function getStartTimeAttr($value, $data)
    {
        return date('Y-m-d H:i', strtotime($data['start_time']));
    }

    public function getEndTimeAttr($value, $data)
    {
        return date('Y-m-d H:i', strtotime($data['end_time']));
    }

    /**
     * 活动详情
     * @param $Activity_id
     * @return ActivityModel|null
     * @throws BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public static function detail($Activity_id)
    {
        if (!$model = parent::detail($Activity_id)) {
            throw new BaseException(['msg' => '活动不存在']);
        }
        return $model;
    }

    /**
     * 获取活动列表
     * @param int $category_id
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($team_id = 0, $user_id = 0, $limit = 5, $isfront = 0,$shanghui_id = 0)
    {
        if ($user_id > 0) {
            $this->where('team_id', 'in', [$team_id, 0]); //这是管理员看代表处和管理人本队伍的
        } else {
            if($team_id)
            $this->where('team_id', '=', $team_id);       //这是看队伍的
        }
        if ($isfront == 1) {//这是首页列表，只看审核通过的，else是个人中心活动列表，看全部
            $this->where('examine_status', '=', 1);
        }
        return $this->field(['activity_content'], true)
            ->with(['image', 'team'])
            ->where('is_delete', '=', 0)
            ->where('shanghui_id', '=',$shanghui_id)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($limit, false, ['query' => \request()->request()]);
    }

    /**
     * 获取待审核活动列表
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getExamineList($limit = 5,$shanghui_id = 0)
    {

        return $this->field(['activity_content'], true)
            ->with(['image', 'team'])
            ->where('is_delete', '=', 0)
            ->where('examine_status', '=', 0)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($limit, false, ['query' => \request()->request()]);
    }

    /**
     * 新增记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {
        if (empty($data['image_id'])) {
            $this->error = '请上传相关图片';
            return false;
        }
        if (empty($data['activity_content'])) {
            $this->error = '请输入活动详情';
            return false;
        }
        $data['wxapp_id'] = self::$wxapp_id;
        return $this->allowField(true)->save($data);
    }

    /**
     * 更新记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        return $this->allowField(true)->save($data) !== false;
    }

}